package com.tongzhou.tongzhou.account.action;

import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.tongzhou.db.exception.DBException;
import com.tongzhou.tongzhou.account.dao.IAccountDAO;
import com.tongzhou.tongzhou.account.model.Client;
import com.tongzhou.tongzhou.account.model.SearchClientCondition;

public class ClientAction extends ActionSupport{
	private int id;
	private String active;
	private Client client;
	private List<Client> result=null;
	private SearchClientCondition condition=null;
	private int pageSize ;
	private int currentPage ;
	private String orderBy ;
	private String orderByType ;
	
	private IAccountDAO dao=null;
	
	/**
	 * 用户列表界面
	 *@author: 彭定雄
	 *@return 
	 *@throws DBException
	 */
	public String listClient() throws DBException{
		checkSearchCondition();
		condition = new SearchClientCondition();
		if (currentPage == 0){
			currentPage = 1;
		}
		condition.setCurrentPage(currentPage) ;
		condition.setActive(active);
		condition.setOrderBy(orderBy);
		condition.setOrderByType(orderByType) ;
		condition.setPageSize(pageSize) ;
	    result = dao.listClient(condition);
		return "listClient";
	} 
	
	
	/**
	 * 设置列表显示格式
	 *@author: 彭定雄
	 */
	private void checkSearchCondition(){
		if(currentPage <=0){
			currentPage = 1 ;
		}
		
		if(pageSize<=0){
			pageSize = 10 ;
		}
		
		if(orderBy==null||"".equals(orderBy)){
			orderBy = "active,create_time" ;
		}
		
		if(orderByType == null || "".equals(orderByType)){
			orderByType = "desc" ;
		}
	}
	
	public String editClient() throws DBException{
		client=new Client();
		client.setId(id);
		if(active.equals("Y")){
			client.setActive("N");
		}else{
			client.setActive("Y");
		}
		dao.updateClient(client);
		active="";
		listClient();
		return "listClient";
	}
	
	public String deleteClient() throws DBException{
//		dao=new UserMgrDAO();
		dao.deleteClient(id);
		active="";
		listClient();
		return "listClient";
	}


	public int getId() {
		return id;
	}


	public void setId(int id) {
		this.id = id;
	}


	public String getActive() {
		return active;
	}


	public void setActive(String active) {
		this.active = active;
	}


	public Client getClient() {
		return client;
	}


	public void setClient(Client client) {
		this.client = client;
	}


	public List<Client> getResult() {
		return result;
	}


	public void setResult(List<Client> result) {
		this.result = result;
	}


	public SearchClientCondition getCondition() {
		return condition;
	}


	public void setCondition(SearchClientCondition condition) {
		this.condition = condition;
	}


	public int getPageSize() {
		return pageSize;
	}


	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}


	public int getCurrentPage() {
		return currentPage;
	}


	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}


	public String getOrderBy() {
		return orderBy;
	}


	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}


	public String getOrderByType() {
		return orderByType;
	}


	public void setOrderByType(String orderByType) {
		this.orderByType = orderByType;
	}


	public IAccountDAO getDao() {
		return dao;
	}


	public void setDao(IAccountDAO dao) {
		this.dao = dao;
	}
}
